package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.CheckPageModel;
import com.southwind.common.ResultObj;
import com.southwind.entity.BusCar;
import com.southwind.entity.BusCheck;
import com.southwind.entity.BusCustomer;
import com.southwind.entity.BusRent;
import com.southwind.mapper.BusCarMapper;
import com.southwind.mapper.BusCheckMapper;
import com.southwind.mapper.BusRentMapper;
import com.southwind.service.BusCarService;
import com.southwind.service.BusCheckService;
import com.southwind.service.BusCustomerService;
import com.southwind.service.BusRentService;
import com.southwind.util.RandomUtils;
import com.southwind.vo.StatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-05-28
 */
@RestController
@RequestMapping("/check")
public class BusCheckController {

    @Autowired
    private BusRentMapper rentMapper;
    @Autowired
    private BusCarMapper carMapper;
    @Autowired
    private BusRentService rentService;
    @Autowired
    private BusCarService carService;
    @Autowired
    private BusCustomerService customerService;
    @Autowired
    private BusCheckService checkService;
    @Autowired
    private BusCheckMapper checkMapper;

    @GetMapping("/opernameYearGradeStat")
    public Map opernameYearGradeStat(){
        List<StatVO> list = rentMapper.opernameYearGradeStat();
        List<String> name = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        for (StatVO statVO : list) {
            name.add(statVO.getName());
            value.add(statVO.getValue());
        }
        Map map = new HashMap();
        map.put("name", name);
        map.put("value", value);
        return map;
    }

    @GetMapping("/init")
    public ResultObj init(String rentid){
        BusRent rent = this.rentService.getById(rentid);
        if(rent == null) return ResultObj.RENT_ID_ERROR;
        if(rent.getRentflag() == 1) return ResultObj.RENT_CHECK_ERROR;
        BusCar car = this.carService.getById(rent.getCarnumber());
        BusCustomer customer = this.customerService.getById(rent.getIdentity());
        BusCheck check = new BusCheck();
        check.setCheckid(RandomUtils.createRandomStringUseTime("JC"));
        check.setRentid(rentid);
        check.setCheckdate(new Date());
        check.setOpername(rent.getOpername());
        return new ResultObj(0, rent, car, customer, check);
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody BusCheck check){
        BusRent rent = this.rentService.getById(check.getRentid());
        this.carMapper.check(rent.getCarnumber());
        this.rentMapper.updateRentFlag(rent.getRentid());
        boolean save = this.checkService.save(check);
        if(save) return ResultObj.CHECK_ADD_SUCCESS;
        return ResultObj.CHECK_ADD_ERROR;
    }

    @GetMapping("/list")
    public Map list(CheckPageModel checkPageModel){
        Page<BusCheck> page = new Page<>(checkPageModel.getPage(), checkPageModel.getLimit());
        QueryWrapper<BusCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(checkPageModel.getRentid()), "rentid", checkPageModel.getRentid())
                    .like(StringUtils.isNotBlank(checkPageModel.getCheckid()),"checkid",checkPageModel.getCheckid());
        Page<BusCheck> resultPage = this.checkService.page(page, queryWrapper);
        return ResultObj.resultData(resultPage.getRecords(), resultPage.getTotal());
    }

    @GetMapping("/companyYearGradeStat")
    public List<Integer> companyYearGradeStat(){
        return this.checkMapper.companyYearGradeStat();
    }

}

